package com.changanw.tienchin.web.controller.tienchin.clue;


import com.changanw.tienchin.activity.service.IActivityService;
import com.changanw.tienchin.channel.service.IChannelService;
import com.changanw.tienchin.clue.domain.Clue;
import com.changanw.tienchin.clue.domain.vo.ClueDetails;
import com.changanw.tienchin.clue.domain.vo.ClueSummary;
import com.changanw.tienchin.clue.domain.vo.ClueVO;
import com.changanw.tienchin.clue.service.IClueService;
import com.changanw.tienchin.common.annotation.Log;
import com.changanw.tienchin.common.core.controller.BaseController;
import com.changanw.tienchin.common.core.domain.AjaxResult;
import com.changanw.tienchin.common.core.page.TableDataInfo;
import com.changanw.tienchin.common.enums.BusinessType;
import com.changanw.tienchin.common.validator.CreateGroup;
import com.changanw.tienchin.common.validator.EditGroup;
import com.changanw.tienchin.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author changanw
 * @since 2023-11-25
 */
@RestController
@RequestMapping("/tienchin/clue")
public class ClueController extends BaseController {
  @Autowired
  IClueService clueService;
  @Autowired
  IChannelService channelService;
  @Autowired
  IActivityService activityService;
  @Autowired
  ISysUserService userService;

  @PreAuthorize("hasPermission('tienchin:clue:list')")
  @GetMapping("/list")
  public TableDataInfo list(ClueVO clueVO) {
    startPage();
    List<ClueSummary> list = clueService.selectClueList(clueVO);
    return getDataTable(list);
  }
  /**
   * 新增线索
   */
  @PreAuthorize("hasPermission('tienchin:clue:create')")
  @Log(title = "线索管理", businessType = BusinessType.INSERT)
  @PostMapping
  public AjaxResult add(@Validated(CreateGroup.class) @RequestBody Clue clue) {
    return clueService.addClue(clue);
  }

  /**
   * 返回所有渠道
   * @return
   */
  @PreAuthorize("hasPermission('tienchin:clue:create')")
  @GetMapping("/channels")
  public AjaxResult getAllChannel(){
    return AjaxResult.success(channelService.list());
  }

  /**
   * 根据渠道id  获取活动
   * @return
   */
  @PreAuthorize("hasPermission('tienchin:clue:create')")
  @GetMapping("/activity/{channelId}")
  public AjaxResult getActivityByChannelId(@PathVariable Integer channelId){
    return activityService.selectActivityByChannelId(channelId);
  }


  @PreAuthorize("hasPermission('tienchin:clue:assign')")
  @GetMapping("/users/{deptId}")
  public AjaxResult getUsersByDeptId(@PathVariable Long deptId){
    return userService.getUsersByDeptId(deptId);
  }
  @PreAuthorize("hasAnyPermissions('tienchin:clue:list','tienchin:clue:view')")
  @GetMapping("/{clueId}")
  public AjaxResult getClueDetailsById(@PathVariable Integer clueId){
    return clueService.getClueDetailsById(clueId);
  }

  /**
   * 线索跟进
   * @param clueDetails
   * @return
   */
  @PreAuthorize("hasPermission('tienchin:clue:view')")
  @PostMapping("/follow")
  public AjaxResult clueFollow(@RequestBody ClueDetails clueDetails){
    return clueService.clueFollow(clueDetails);
  }

  /**
   * 线索转为商机
   * @param clueId
   * @return
   */
  @PreAuthorize("hasPermission('tienchin:clue:follow')")
  @PostMapping("/toBusiness/{clueId}")
  public AjaxResult clue2Business(@PathVariable Integer clueId){
    return clueService.clue2Business(clueId);
  }
  /**
   * 无效线索
   *
   */
  @PreAuthorize("hasPermission('tienchin:clue:view')")
  @PostMapping("/invalid")
  public AjaxResult invalidClue(@RequestBody ClueDetails clueDetails){
    return clueService.invalidClue(clueDetails);
  }

  @PreAuthorize("hasPermission('tienchin:activity:edit')")
  @GetMapping("/summary/{clueId}")
  public AjaxResult getClueSummaryById(@PathVariable Integer clueId) {
    return clueService.getClueSummaryById(clueId);
  }
  @PreAuthorize("hasPermission('tienchin:activity:edit')")
  @PutMapping
  public AjaxResult updateClue(@Validated(EditGroup.class)@RequestBody Clue clue){
    return clueService.updateClue(clue);
  }

  /**
   * 删除线索
   */
  @PreAuthorize("hasPermission('tienchin:clue:remove')")
  @Log(title = "渠道管理", businessType = BusinessType.DELETE)
  @DeleteMapping("/{clueIds}")
  public AjaxResult remove(@PathVariable Integer[] clueIds) {
    return clueService.deleteClueByIds(clueIds);
  }

}

